using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CardStore : MonoBehaviour
{
    public TextAsset cardData;
    public List<Card> cardList = new List<Card>();
    public List<CharacterCard> characterCardList = new List<CharacterCard>();
    // Start is called before the first frame update
    void Start()
    {
        LoadCardData();
        //TestLoad();
    }

    // Update is called once per frame
    void Update()
    {

    }
    public void LoadCardData()
    {
        string[] dataRow = cardData.text.Split('\n');
        foreach (var row in dataRow)
        {
            string[] rowArray = row.Split(',');
            if (rowArray[0] == "#")
            {
                continue;
            }
            else if (rowArray[0] == "attack")
            {
                //新建攻击卡
                int id = int.Parse(rowArray[1]);
                string name = rowArray[2];
                int atk = int.Parse(rowArray[3]);
                int mana = int.Parse(rowArray[4]);
                AttackCard attackCard = new AttackCard(id, name, atk, mana);
                cardList.Add(attackCard);

                //Debug.Log("读取到攻击卡：" + monsterCard.cardName);
            }
            else if (rowArray[0] == "spell")
            {
                //新建魔法卡
                int id = int.Parse(rowArray[1]);
                string name = rowArray[2];
                string effect = rowArray[3];
                int effectvalue = int.Parse(rowArray[4]);
                int mana = int.Parse(rowArray[5]);
                SpellCard spellCard = new SpellCard(id, name, effect, effectvalue, mana);
                cardList.Add(spellCard);
            }
            else if (rowArray[0] == "character")
            {
                // 加载角色卡
                int id = int.Parse(rowArray[1]);
                string name = rowArray[2];
                int health = int.Parse(rowArray[3]);
                int mana = int.Parse(rowArray[4]);
                string skill = rowArray[5];
                int shield = int.Parse(rowArray[6]);
                CharacterCard characterCard = new CharacterCard(id, name, health, mana, skill, shield);
                characterCardList.Add(characterCard);
            }
            else if (rowArray[0] == "shield")
            {
                // 加载护盾卡
                int id = int.Parse(rowArray[1]);
                string name = rowArray[2];
                int shield = int.Parse(rowArray[3]);
                int mana = int.Parse(rowArray[4]);
                ShieldCard shieldCard = new ShieldCard(id, name, shield, mana);
                cardList.Add(shieldCard);
            }
        }
    }
    //测试卡牌是否进去卡包
    public void TestLoad()
    {
        foreach (var card in cardList)
        {
            Debug.Log("卡牌：" + card.id.ToString() + card.cardName);
        }
        foreach (var CharacterCard in characterCardList)
        {
            Debug.Log("卡牌：" + CharacterCard.id.ToString() + CharacterCard.cardName);
        }
    }

    public Card RandomCard()
    {
        //随机从cardList取一张卡
        Card card = cardList[Random.Range(0, cardList.Count)];
        return card;
    }
    public CharacterCard RandomCharacterCard()
    {
        if (characterCardList.Count > 0)
        {
            CharacterCard randomCard = characterCardList[Random.Range(0, characterCardList.Count)];
            return randomCard;
        }

        Debug.LogWarning("没有可用的角色卡！");
        return null;
    }

    public Card CopyCard(int _id)
    {
        if (_id < cardList.Count)
        {
            // 普通卡牌
            Card originalCard = cardList[_id];
            if (originalCard is AttackCard)
            {
                var attackCard = originalCard as AttackCard;
                return new AttackCard(attackCard.id, attackCard.cardName, attackCard.attack, attackCard.mana);
            }
            else if (originalCard is SpellCard)
            {
                var spellCard = originalCard as SpellCard;
                return new SpellCard(spellCard.id, spellCard.cardName, spellCard.effect, spellCard.effectvalue, spellCard.mana);
            }
            else if (originalCard is ShieldCard)
            {
                var shieldCard = originalCard as ShieldCard;
                return new ShieldCard(shieldCard.id, shieldCard.cardName, shieldCard.Shield, shieldCard.mana);
            }

        }
        // 角色卡
        if (_id < characterCardList.Count)
        {
            var characterCard = characterCardList[_id];
            return new CharacterCard(characterCard.id, characterCard.cardName, characterCard.healthPoint, characterCard.mana, characterCard.skill, characterCard.shield);
        }

        Debug.LogError("无效的卡牌 ID: " + _id);
        return null;
    }
}
